package com.wd.live.module_nearby


import android.content.pm.PackageManager
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.amap.api.maps2d.AMap
import com.amap.api.maps2d.MapView
import com.amap.api.maps2d.model.MyLocationStyle
import com.amap.api.services.core.PoiItem
import com.amap.api.services.poisearch.PoiResult
import com.amap.api.services.poisearch.PoiSearch
import com.wd.live.module_nearby.databinding.ActivityBivariateMapBinding


@Route(path = "/nearby/bivariate_map_activity")
class BivariateMapActivity : AppCompatActivity(),PoiSearch.OnPoiSearchListener,AMapLocationListener {
    private lateinit var binding:ActivityBivariateMapBinding
    private lateinit var mapView: MapView
    private lateinit var aMap: AMap
    private lateinit var myLocationStyle: MyLocationStyle
    //定位
    private lateinit var mLocationClient: AMapLocationClient
    //
    private  var mLocationListener: AMapLocationListener=this

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding=ActivityBivariateMapBinding.inflate(layoutInflater)
        setContentView(binding.root)
        AMapLocationClient.updatePrivacyShow(this, true, true)
        AMapLocationClient.updatePrivacyAgree(this, true)
        //
        mLocationClient= AMapLocationClient(applicationContext)
        mLocationClient.setLocationListener(mLocationListener)
        mapView=binding.map
        mapView.onCreate(savedInstanceState)
        //初始化地图控制器
        aMap = mapView.map
        //进行地图相关设置和操作


        setLocation()
        //
        checkLocationPermission()
    }

    private fun checkLocationPermission() {
        if (ContextCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){

        }
    }

    private fun setLocation() {

        //
        val option=AMapLocationClientOption()

        option.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn)
        if (null!=mLocationClient){
            mLocationClient.setLocationOption(option)
            mLocationClient.stopLocation()
            mLocationClient.startLocation()
        }
        //
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy)

        option.setInterval(1000)
        option.setNeedAddress(true)
        option.setMockEnable(true)
        mLocationClient.setLocationOption(option)
        mLocationClient.startLocation()
        //显示定位的方法
        myLocationStyle= MyLocationStyle()
        myLocationStyle.interval(2000)//设置定位间隔
        aMap.setMyLocationStyle(myLocationStyle)
        aMap.isMyLocationEnabled=true
        //连续定位
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW)

    }

    //
    override fun onDestroy() {
        super.onDestroy()
        mapView.onDestroy()
    }
    //
    override fun onResume() {
        super.onResume()
        mapView.onResume()
    }
    //
    override fun onPause() {
        super.onPause()
        mapView.onPause()
    }
    //
    override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
        super.onSaveInstanceState(outState, outPersistentState)
        mapView.onSaveInstanceState(outState)
    }

    override fun onPoiSearched(p0: PoiResult?, p1: Int) {
        TODO("Not yet implemented")
    }

    override fun onPoiItemSearched(p0: PoiItem?, p1: Int) {
        TODO("Not yet implemented")
    }

    override fun onLocationChanged(amapLocation : AMapLocation?) {
        if (amapLocation !=null){
            if ( amapLocation.errorCode==0){
                //可在其中解析amapLocation获取相应内容。
                Toast.makeText(this,amapLocation.latitude.toString()+"\n"+amapLocation.longitude.toString(),Toast.LENGTH_LONG).show()

            }else{
                //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                Log.e("AmapError","location Error, ErrCode:"
                        + amapLocation.getErrorCode() + ", errInfo:"
                        + amapLocation.getErrorInfo());
            }

        }
    }

}